<template>
	<view class="page dark-page">

		<view class="system">
			<view class="left">
				<view class="title">
					跟随系统
				</view>
				<view class="subTitle">
					开启后,将跟随系统打开或关闭深色模式
				</view>
			</view>
			<switch color="#53786C" background-color="#fff" :checked="autoMode" @change="changeAuto" style="transform:scale(0.7" />
		</view>

		<view v-if="showCustom" class="custom">
			<radio-group class="uni-list">
				<view class="uni-list-cell uni-list-cell-pd" v-for="(item,index) in options" :key="index" @click="changeMode(item.value)">
					<view>
						<radio active-background-color="#53786C"   :value="item.value" :checked="item.value === darkMode"></radio>
					</view>
					<label class="label-2-text">
						<text>{{item.text}}</text>
					</label>
				</view>
			</radio-group>
		</view>
	</view>
</template>

<script>
	const DARK_MODE = 'mode##dark'
	const AUTO_MODE = 'ode##auto'
	const theme = uni.getSystemInfoSync().theme
	const initDark = getStorage(DARK_MODE)
	const initAuto = getStorage(AUTO_MODE) === false ? false : true
	const darkIcon = {
		color: '#3cc51f',
		size: '22',
		type: 'color-filled',
	}
	const autoIcon = {
		color: '#3cc51f',
		size: '22',
		type: 'color',
	}

	function getStorage(key) {
		return uni.getStorageSync(key)
	}

	function setStorage(key, value) {
		uni.setStorageSync(key, value)
	}
	


	export default {
		data() {
			return {
				darkIcon,
				autoIcon,
				autoMode: initAuto,
				darkMode: initDark,
				options: [{
						value: 'light',
						text: '浅色模式'
					},
					{
						value: 'dark',
						text: '深色模式'
					},
				],
				showAD: false
			}
		},
		computed: {
			showCustom() {
				return this.autoMode === false
			},
			isShowAd() {
				return this.showAD

			},
		},
		watch: {
			autoMode(val, old) {
				if (val !== old) {
					setStorage(AUTO_MODE, val)
					this.setUIStyle(val ? 'auto' : this.darkMode)
				}
			},
			darkMode(val, old) {
				if (val !== old) {
					const autoMode = this.autoMode ? 'auto' : val
					setStorage(AUTO_MODE, false)
					setStorage(DARK_MODE, val)
					this.setUIStyle(autoMode)
				}
			}
		},
		onLoad() {
			this.getMode()
		},
		onNavigationBarButtonTap(e) {
			
			uni.showModal({
				title:"提示",
				content:"新设置将在重启app后生效,是否继续?",
				cancelText:"取消",
				confirmText:"确定",
				success(res) {
					if (res.confirm) {
						plus.runtime.restart();
					} else {
				
					}
				
				}
			})
			
		},
		methods: {
			getMode() {
				const mode = getStorage(DARK_MODE) || theme
				const auto = getStorage(AUTO_MODE) === false ? false : true

				this.autoMode = auto
				this.darkMode = mode
			},
			setUIStyle(style) {
				plus.nativeUI.setUIStyle(style)
			},
			changeAuto() {
				this.autoMode = !this.autoMode
			},
			changeMode(val) {
				console.log("sssss=",val);
				this.darkMode = val
			}
		}
	}
</script>

<style lang="scss">
	.system {
		display: flex;
		position: relative;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 20rpx;
		margin-top: 40rpx;
		background-color: white;

		.left {
			.title {
				font-size: 30rpx;
				color: #333;
			}

			.subTitle {
				font-size: 26rpx;
				color: #999;
			}
		}

	}
	.custom{
		margin-top:40rpx;
		.uni-list-cell {
			display: flex;
			justify-content: flex-start;
			padding: 40rpx 20rpx;
			border-bottom: 2rpx solid #f9f9f9;
			background-color: white;
		}
		
		.uni-list .label-3 {
			padding: 60rpx 0;
		}
		
		.label-2-text {
			flex: 1;
		}
	}

	


	.dark-page {
		position: fixed;
		width: 100vw;
		height: 100vh;
		background-color: #f1f1f1;

		.custom-title {
			padding-left: 20rpx;
			margin-top: 40rpx;
			font-size: 30rpx;
			color: #333;
			background-color: #eee;
		}

		.radio-list {
			padding: 0 60rpx;
		}

		.radio-list .uni-list-item :active {
			background-color: #d9d9d9;
			border-radius: 4px;
		}

		@media (prefers-color-scheme: dark) {

			.custom,
			.radio-list {
				color: #fff !important;
				background-color: #121212 !important;
			}

			:deep .custom-title {
				background-color: #222 !important;
			}

			:deep .custom-title text {
				color: #fff !important;
			}
		}
	}
</style>